//
//  BaseViewController.m
//  Penglai
//
//  Created by intel intel on 12-5-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ShowHomeBaseViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "GlobalVar.h"

@implementation ShowHomeBaseViewController
@synthesize titleImage;
@synthesize httpRequest;

- (void)dealloc {
    
    [httpRequest clearDelegatesAndCancel];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
 
}

-(void)showMHUD:(NSString *)msg
{
    [self hideMHUD];
    MHUD = [[MBProgressHUD alloc] initWithView:self.view];
	[self.view addSubview:MHUD];
    MHUD.removeFromSuperViewOnHide = YES;
    //[MHUD setLabelFont:[UIFont systemFontOfSize:12]];
	[MHUD setLabelText:msg];
	[MHUD show:YES];
}

-(void)hideMHUD
{
    if (MHUD) {
        [MHUD hide:YES];
        MHUD=nil;
    }
}

-(void)hideMHUD:(NSString *)msg success:(BOOL)success
{
    if (success) {
        MHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
    }else{
        MHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-warning.png"]];
    }
    MHUD.mode = MBProgressHUDModeCustomView;
    MHUD.labelText = msg;
    [MHUD hide:YES afterDelay:1];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

}

-(BottomView *)addBottomView
{
    CGFloat y = self.view.frame.size.height - 60;
    BottomView *bottomView = [[[NSBundle mainBundle] loadNibNamed:@"BottomView" owner:self options:nil]lastObject];
    bottomView.frame = CGRectMake(0, y, self.view.bounds.size.width, 60);
    [self.view addSubview:bottomView];
    bottomView.btn_camera.hidden = YES;
    bottomView.btn_all_power.hidden = YES;
    //bottomView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    return bottomView;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    
    if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDevStatusChangeData:) name:NOTIFICATION_DEV_STATE_CHANGE_GET object:nil];
    
    //self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        UIImage *image = [UIImage imageNamed: @"bg_top"];
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }else{
        UIImage *image = [UIImage imageNamed: @"bg_top"];
        CALayer *nbLayer =  self.navigationController.navigationBar.layer;
        nbLayer.contents = (id)image.CGImage;
    }
    
    
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setTitleTextAttributes:)]) {
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor darkGrayColor],UITextAttributeTextColor,[UIColor whiteColor],UITextAttributeTextShadowColor, nil]];
    }
    
    
    if ([self.navigationController.viewControllers count]>1) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.frame = CGRectMake(0, 0, 55, 30);
        backBtn.showsTouchWhenHighlighted = YES;
        [backBtn setImage:[UIImage imageNamed:@"btn_top2.png"] forState:UIControlStateNormal];
        [backBtn setImage:[UIImage imageNamed:@"btn_top2_selected.png"] forState:UIControlStateHighlighted];
        [backBtn addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
        self.navigationItem.leftBarButtonItem = backItem;
    }
    
    activity = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
    activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:activity];

    CGRect frame;
    if (iPhone5) {
        frame = CGRectMake(0, 0, 320, 548-44);
    }else{
        frame = CGRectMake(0, 0, 320, 460-44);
    }
    self.view.frame = frame;
  
    //self.view.backgroundColor = [UIColor colorWithRed:52.0f/255 green:52.0f/255 blue:52.0f/255 alpha:1];
}

- (void)updateDevStatusChangeData:(NSNotification *)data {
    
}

-(UIButton *)createNavButton:(NSString *)title action:(SEL)action
{
    UIButton *cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cityButton.frame = CGRectMake(0, 0, 50, 40);
    
    UIImage *img = [UIImage imageNamed:@"common_button"];
    UIImage *img1 = [UIImage imageNamed:@"common_button"];
    
    [cityButton setBackgroundImage:img forState:UIControlStateNormal];
    [cityButton setBackgroundImage:img1 forState:UIControlStateHighlighted];
    
    [cityButton setTitle:title forState:UIControlStateNormal];
    cityButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [cityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [cityButton addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    cityButton.showsTouchWhenHighlighted = YES;
    return cityButton;
}

-(UIButton *)createNavigationImgButton:(NSString *)img action:(SEL)action
{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage *image = [UIImage imageNamed:img];
    
    backBtn.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    backBtn.showsTouchWhenHighlighted = YES;
    backBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [backBtn setImage:image forState:UIControlStateNormal];
    [backBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return backBtn;
}

-(UIBarButtonItem*)createNavItem:(NSString *)title action:(SEL)action
{
    UIButton *cityButton = [self createNavButton:title action:action];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:cityButton];
    return item;
}

-(void)back:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)addKeyboardNotification:(UIView *)_keybordChageView
{
    keybordChageView = _keybordChageView;
    frameKeybordChageView = _keybordChageView.frame;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    #ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    #endif
}
-(void)removeKeyboardNotification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    #ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    #endif
}

-(void) keyboardWillShow:(NSNotification *)note{
	CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];
    
    CGRect containerFrame = keybordChageView.frame;
    int height = self.view.frame.size.height- keyboardBounds.size.height;
    containerFrame.size.height = height;
    
    [UIView beginAnimations:nil context:NULL];
	[UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    keybordChageView.frame = containerFrame;
	[UIView commitAnimations];
}

-(void) keyboardWillHide:(NSNotification *)note{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];
    keybordChageView.frame = frameKeybordChageView;
	[UIView commitAnimations];
}

- (void)toastMsg:(NSString *)msg {

}

- (void)toastMsg:(NSString *)msg duration:(NSTimeInterval)timerInterval position:(MsgToastPosition)position {
    
    switch (position) {
        case MsgToastPositionCenter:
            [self.view makeToast:msg duration:timerInterval position:@"center"];
            break;
        case MsgToastPositionBottom:
            [self.view makeToast:msg duration:timerInterval position:@"bottom"];
            break;
        case MsgToastPositionTop:
            [self.view makeToast:msg duration:timerInterval position:@"top"];
            break;
            
        default:
            break;
    }
    

}

@end

